<?php
namespace app\system\model;

use think\Model;

class LoginModel extends Model
{
    /**
     * 管理员登录
     * @param array $data
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkLogin(array $data)
    {
        $username = $data['username'];
        $password = $data['password'];

        $result = $this->table('__MANAGE__')->alias('m')
            ->join('__GROUP__ g', 'g.id=m.group_id')
            ->where(['m.username'=>$username,'m.locks'=>0])
            ->field('m.*,g.title groupname')
            ->find();

        if (!empty($result)) {
            $result = $result->toArray();
            if (true) {
                $update['lasttime'] = time();
                $update['lastip'] = request()->ip();
                $this->table('__MANAGE__')->where('id',$result['id'])->update($update);
                return ['status' => "y", 'manageInfo' => $result];
            } else {
                return ['status' => "n",'info' => '密码错误'];
            }
        }else
        {
            return ['status' => "n",'info' => '用户名不存在'];
        }
    }
}